package cn.com.cis.web.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cis.domain.JobLog;
import cn.com.cis.plugins.mybatis.PageInfo;
import cn.com.cis.service.ETLExecutorService;
import cn.com.cis.service.JobLogService;

@Controller
@RequestMapping(value = "/log")
public class LogController {

    

    @Autowired
    private JobLogService logService;
    
    private void setNavigateBar(ModelAndView mv){
    	mv.addObject("icon", "icon-list");
        mv.addObject("title", "执行日志");
    }

    /*
    
    @Autowired
    private ETLServer etlServer;
    
    // tomcat call method
    @RequestMapping(value = "/power", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public double getPower() {
        return etlServer.getPower() ;
    }

    @RequestMapping(value = "/termination", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public boolean termination() throws InterruptedException {
        etlServer.shutdownNow();
        return true;
    }
    */

    @RequiresPermissions("执行日志:查看日志")
    @RequestMapping(value = "/list/{page}", method = RequestMethod.GET)
    public ModelAndView list(@PathVariable("page") int page) {
        ModelAndView mav = new ModelAndView("log/list");
        setNavigateBar(mav);
        
        List<JobLog> list = logService.selectAllJobLog(page,10);
        PageInfo<JobLog> pageInfo = new PageInfo<JobLog>(list,10);
        
        Map<String, String> runningJobAddress = new HashMap<String, String>();
        for(java.util.concurrent.ExecutorService eService : ETLExecutorService.getExecutorServices()){
        	if(!eService.isTerminated()){
        		runningJobAddress.put(eService.toString(), eService.toString());
        	}
        }
        
        mav.addObject("list", list);
        mav.addObject("pageInfo",pageInfo);
        mav.addObject("runningJobAddress", runningJobAddress);
        
        return mav;
    }


    @RequiresPermissions("执行日志:查看日志")
    @RequestMapping(value = "/ajaxlist", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<JobLog> ajaxlist(@RequestParam(value = "page",defaultValue = "1",required = false) int page) {
        List<JobLog> list = logService.selectAllJobLog(page,7);
        PageInfo<JobLog> pageInfo = new PageInfo<JobLog>(list,7);
        return pageInfo;
    }
}
